/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package tuoc;

import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.print.PageFormat;
import java.awt.print.Printable;
import java.awt.print.PrinterException;
import java.awt.print.PrinterJob;
import java.sql.*;
import java.text.DecimalFormat;
import javax.sql.*;
import java.util.*;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JRootPane;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;

/**
 *
 * @author apple
 */
public class Tuoc {

    /**
     * @param args the command line arguments
     */
    
    store_model storeModel = new store_model();
    orders_model ordersModel = new orders_model();
    products_model productsModel = new products_model();
    order_detail_model orderDetailModel = new order_detail_model();
    manage_view manageView = new manage_view();
    static index_view view;
    
    public static void main(String[] args) {
        // TODO code application logic here
        
        //        GET ALL PRODUCTS
        Vector allproducts = new products_model().get_elements();

        
//        GET ALL TABLES
        Vector allTable = new tables_model().get_elements();
        
        //        CREATE AND ADD TAB TO VIEW
        view = new index_view();
        Iterator tableIter = allTable.iterator();
        while(tableIter.hasNext())
        {
            tables_model currenItem = (tables_model)tableIter.next();
            form_view tabView = new form_view();
            tabView.set_search_table_data(allproducts);
            tabView.table_id = currenItem.id;
            tabView.reloadTableOrder();
            view.add_tab_view(currenItem.name,tabView);
        }
        view.show();
        view.setExtendedState(JFrame.MAXIMIZED_BOTH);

    }
    
    
    public void insertStore(String tableId,String productId,String quantity)
    {
        Map<String,String> data = new HashMap<String, String>();
        data.put("table_id", tableId);
        data.put("product_id", productId);
        
        
        if(storeModel.checkExist(productId, tableId))
        {
            int qty = storeModel.get_element_by_table_product(productId, tableId).quantity;
            data.put("quantity", (qty + Integer.parseInt(quantity)) + "");
            storeModel.update_data(data);
        }
        else
        {
            data.put("quantity", quantity);
            storeModel.insert_data(data);
        }
    }
    
    public void increaseStoreQuantity(String tableId,String productId,String quantity)
    {
        if(storeModel.checkExist(productId, tableId))
        {
            int qty = storeModel.get_element_by_table_product(productId, tableId).quantity;
            
            Map<String,String> data = new HashMap<String, String>();
            data.put("table_id", tableId);
            data.put("product_id", productId);
            data.put("quantity", (qty + Integer.parseInt(quantity)) + "");
            
            storeModel.update_data(data);
        }
    }
    
    public Vector getStoreData(String tableId)
    {
        Vector result = storeModel.get_elements_by_table(tableId);
        return result;
    }
    
    public void clearStoreData(String tableId)
    {
        storeModel.delete_by_table(tableId);
    }
    
    public String priceWithoutDecimal (double price) {
        DecimalFormat formatter = new DecimalFormat("###,###,###.##");
        return formatter.format(price);
    }
    
    public void insertOrderData(TableModel orderTableModel,String tableId,String total)
    {
        Map<String,String> data = new HashMap<String, String>();
        data.put("table_id", tableId);
        data.put("total", total);
        data.put("created_date", new java.util.Date().getTime()+ "");
        int inserted_id = ordersModel.insert_data(data);
        
        Vector store = storeModel.get_elements_by_table(tableId);
        Iterator storeIter = store.iterator();
        while(storeIter.hasNext())
        {
            store_model currentObject = (store_model)storeIter.next();
            Map<String,String> orderDetailData = new HashMap<String, String>();
            orderDetailData.put("order_id", inserted_id + "");
            orderDetailData.put("product_id", currentObject.product_id + "");
            orderDetailData.put("quantity", currentObject.quantity + "");
            orderDetailData.put("sub_total", (currentObject.get_subTotal()) + "");
            orderDetailModel.insert_data(orderDetailData);
        }
        
        this.clearStoreData(tableId);
        this.printComponenet();
    }
    
    public void printComponenet()
    {
//        PrinterJob pj = PrinterJob.getPrinterJob();
//        pj.setJobName(" Print Component ");
//
//        pj.setPrintable (new Printable() {    
//          public int print(Graphics pg, PageFormat pf, int pageNum){
//            if (pageNum > 0){
//            return Printable.NO_SUCH_PAGE;
//            }
//
//            Graphics2D g2 = (Graphics2D) pg;
//            g2.translate(pf.getImageableX(), pf.getImageableY());
//            form_view view = new form_view();
//            view.paint(g2);
//            return Printable.PAGE_EXISTS;
//          }
//        });
//        if (pj.printDialog() == false)
//        return;
//
//        try {
//              pj.print();
//        } catch (PrinterException ex) {
//              // handle exception
//        }
        
                final JPanel panel = new JPanel();
            // layout your panel as desired...

            // set the size & location of the panel
            panel.setLocation(0, 0);
            panel.setSize(500, 400);

            PrinterJob job = PrinterJob.getPrinterJob();

            // set the Printable to the PrinterJob
            job.setPrintable(new Printable() {
              public int print(Graphics graphics, PageFormat pageFormat, int pageIndex) {
                if (pageIndex == 0) {
                  panel.print(graphics);
                  return Printable.PAGE_EXISTS;
                }
                return Printable.NO_SUCH_PAGE;
              }
            });

            // show the dialog
            if (job.printDialog()) {
              try {
                job.print();
              }
              catch (PrinterException ex) {
                // handle exception
              }
            }
    }
    
    public void show_manage_view()
    {
//        manage_view manageView = new manage_view();
        manageView.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
        manageView.show();
    }
    
    public void reloadView()
    {
//        view.dispose();
        //        GET ALL PRODUCTS
        Vector allproducts = new products_model().get_elements();

        
//        GET ALL TABLES
        Vector allTable = new tables_model().get_elements();
        
        //        CREATE AND ADD TAB TO VIEW
        view = new index_view();
        Iterator tableIter = allTable.iterator();
        while(tableIter.hasNext())
        {
            tables_model currenItem = (tables_model)tableIter.next();
            form_view tabView = new form_view();
            tabView.set_search_table_data(allproducts);
            tabView.table_id = currenItem.id;
            tabView.reloadTableOrder();
            view.add_tab_view(currenItem.name,tabView);
        }
        view.show();
        view.setExtendedState(JFrame.MAXIMIZED_BOTH);
    }
    
}
